package dl.jf.web.action.admin;

import java.util.ArrayList;
import java.util.List;

import dl.jf.common.model.LabelValue;
import dl.jf.common.model.MUser;
import dl.jf.service.iservice.admin.IAdminMemberService;
import dl.jf.web.util.Toolkit;
import dl.jf.web.action.BaseAction;

public class AdminMemberEditAction extends BaseAction {


	/**
	 * 
	 */
	private static final long serialVersionUID = -6816679202686666282L;

	private static final String POP_MEMBER_CREATE = "popAdminMemberCreate";

	private static final String POP_MEMBER_EDIT = "popAdminMemberEdit";


	public AdminMemberEditAction() {
		super("AdminMemberAction");
	}

	private IAdminMemberService adminMemberService;

	public IAdminMemberService getAdminMemberService() {
		return adminMemberService;
	}

	public void setAdminMemberService(IAdminMemberService adminMemberService) {
		this.adminMemberService = adminMemberService;
	}

	
	private String newUserId;
	
	private Integer newRoleId;
	
	private String newNickname;

	private String newEmailAddress;
	
	private Integer newDisabled;
	
	private String newinsertDate;
	
	private String newPhotoId;
	
	private String editUserId;
	
	private String target;
	
	private List<String> photoList = new ArrayList<String>();

	private List<LabelValue> roleList = new ArrayList<LabelValue>();

	private List<LabelValue> disabledList = new ArrayList<LabelValue>();
	
	public List<String> getPhotoList() {
		if(Toolkit.isEmpty(photoList)){
			pushList();
		}
		return photoList;
	}

	public void setPhotoList(List<String> photoList) {
		this.photoList = photoList;
	}

	public List<LabelValue> getRoleList() {
		if(Toolkit.isEmpty(roleList)){
			pushList();
		}
		return roleList;
	}

	public void setRoleList(List<LabelValue> roleList) {
		this.roleList = roleList;
	}

	public List<LabelValue> getDisabledList() {
		if(Toolkit.isEmpty(disabledList)){
			pushList();
		}
		return disabledList;
	}

	public void setDisabledList(List<LabelValue> disabledList) {
		this.disabledList = disabledList;
	}

	public String getNewUserId() {
		return newUserId;
	}

	public void setNewUserId(String newUserId) {
		this.newUserId = newUserId;
	}

	public Integer getNewRoleId() {
		return newRoleId;
	}

	public void setNewRoleId(Integer newRoleId) {
		this.newRoleId = newRoleId;
	}

	public String getNewNickname() {
		return newNickname;
	}

	public void setNewNickname(String newNickname) {
		this.newNickname = newNickname;
	}

	public String getNewEmailAddress() {
		return newEmailAddress;
	}

	public void setNewEmailAddress(String newEmailAddress) {
		this.newEmailAddress = newEmailAddress;
	}

	public Integer getNewDisabled() {
		return newDisabled;
	}

	public void setNewDisabled(Integer newDisabled) {
		this.newDisabled = newDisabled;
	}

	public String getNewinsertDate() {
		return newinsertDate;
	}

	public void setNewinsertDate(String newinsertDate) {
		this.newinsertDate = newinsertDate;
	}

	public String getNewPhotoId() {
		return newPhotoId;
	}

	public void setNewPhotoId(String newPhotoId) {
		this.newPhotoId = newPhotoId;
	}

	public String getEditUserId() {
		return editUserId;
	}

	public void setEditUserId(String editUserId) {
		this.editUserId = editUserId;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}


	@Override
	public String execute() throws Exception {
		return popMemberCreate();
	}

	private void pushList() {
		List<LabelValue> roleLabelList = null;
		try {
			roleLabelList = adminMemberService.getRoleLabelListById();
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		}
		if (Toolkit.isEmpty(roleLabelList)) {
			roleLabelList = new ArrayList<LabelValue>();
		}
		setRoleList(roleLabelList);

		List<LabelValue> disabledList = new ArrayList<LabelValue>();
		disabledList.add(new LabelValue(getText("msg_admin_member_disabled_no"), "0"));
		disabledList.add(new LabelValue(getText("msg_admin_member_disabled_yes"), "1"));
		setDisabledList(disabledList);

	}
	
	public String popMemberCreate() throws Exception {
		return POP_MEMBER_CREATE;
	}

	public String doMemberCreate() throws Exception {
		MUser user = new MUser();
		user.setUserId(getNewUserId());
		user.setUserName(getNewNickname());
		// default password is set same as user id
		user.setPassword(getNewUserId());
		user.setEmailAddress(getNewEmailAddress());
		user.setRoleId(getNewRoleId());
		user.setDisabled(getNewDisabled());
		if (Toolkit.isEmpty((String) session.get("userId"))) {
			getContext().put("nopermit", "true");
			return POP_MEMBER_CREATE;
		}
		user.setLoginUserId((String) session.get("userId"));
		String id = null;
		try {
			id = adminMemberService.insertUser(user);
		} catch (Exception ex) {
			logger.error("insert user error", ex);
			getContext().put("error", "true");
		}
		getContext().put("createid", id);
		return POP_MEMBER_CREATE;
	}

	public String doMemberEdit() throws Exception {
		MUser user = new MUser();
		user.setUserId(getNewUserId());
		user.setUserName(getNewNickname());
		// default password is set same as user id
		user.setPassword(getNewUserId());
		user.setEmailAddress(getNewEmailAddress());
		user.setRoleId(getNewRoleId());
		user.setDisabled(getNewDisabled());
		if (Toolkit.isEmpty((String) session.get("userId"))) {
			getContext().put("nopermit", "true");
			return POP_MEMBER_EDIT;
		}
		user.setLoginUserId((String) session.get("userId"));
		int count = 0;
		try {
			count = adminMemberService.updateUserByUserId(user);
			if (count <= 0) {
				getContext().put("nouser", "true");
				return POP_MEMBER_EDIT;
			}
		} catch (Exception ex) {
			logger.error("insert user error", ex);
			getContext().put("error", "true");
		}
		getContext().put("editcount", count);
		return POP_MEMBER_EDIT;
	}
	
}
